home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15514 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: mail2news.demon.co.uk!j-bg.demon.co.uk
  2. From: John Sargent <jb@j-bg.demon.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Binary Converter
  5. Date: Fri, 05 Apr 96 22:47:11 GMT
  6. Message-ID: <828744431snz@j-bg.demon.co.uk>
  7. References: <31618729.6BC22935@oberon.hs.gettysburg.edu>
  8. Reply-To: jb@j-bg.demon.co.uk
  9. X-NNTP-Posting-Host: j-bg.demon.co.uk
  10. X-Newsreader: Demon Internet Simple News v1.29
  11. X-Mail2News-Path: j-bg.demon.co.uk
  12.  
  13. In article <31618729.6BC22935@oberon.hs.gettysburg.edu>  ** none **   writes:
  14.  
  15. > I am working on a binary converter function, and it isn't working
  16. > correctly.  I am basically passing a number to be converted to the
  17. > function, then using if statements to see which place of the number
  18. > would be in, dividing, and then doing the same with the remainder, until
  19. > the ones place.  Each time the number fits one of the if statements, it
  20. > copies a one to a string, and for every if it does not fit, it copies a
  21. > zero. Lastly it returns the converted binary number.  Does anyone know a
  22. > better way of doing this?  Or why mine would not be working (I would
  23. > include the source here, but I had some trouble, and had to reformat my
  24. > drive, so it is gone as of now).  If you can help, please email me at
  25. > mnicastr@oberon.hs.gettysburg.edu
  26. > Thanks
  27.  
  28. void ToBin(unsigned int in, char * out)
  29. {
  30.     unsigned mask = 0x8000;
  31.     int bit;
  32.  
  33.     *out = 0;
  34.  
  35.     for ( bit = 0; bit < 16; bit++)
  36.     {
  37.         if ( in & mask )
  38.             strcat(out, "1");
  39.         else
  40.             strcat(out, "0");
  41.  
  42.         mask = mask >> 1;
  43.     }
  44. }
  45.  
  46.  
  47. Regards,
  48. John Sargent
  49.